import 'dart:convert';

import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_picker/picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pin_code_fields/pin_code_fields.dart';

import '../config/config.dart';
import '../entity/order_detail_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../utils/common_utils.dart';
import '../utils/singleton_manager.dart';
import '../views/dialog_manager.dart';
import 'package:crypto/crypto.dart';

///
/// 订单详情页面
///
class OrderDetailPage extends StatefulWidget {
  var orderNo = '';

  OrderDetailPage({Key? key, this.orderNo = ''}) : super(key: key);

  @override
  State<OrderDetailPage> createState() => _OrderDetailPageState();
}

class _OrderDetailPageState extends State<OrderDetailPage> {
  OrderDetailEntity entity = OrderDetailEntity();
  String orderStatusSub = '';
  String orderStatusIcon = '';
  var payPwd = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    orderDetail();
  }

  bool isShowPayBtn(String status) {
    if (status == '5') {
      return true;
    } else {
      return false;
    }
  }

  bool isShowTixingBtn(String status) {
    if (status == '15') {
      return true;
    } else {
      return false;
    }
  }

  bool isShowCancelBtn(String status) {
    if (status == '5') {
      return true;
    } else {
      return false;
    }
  }

  bool isShowReceiptBtn(String status) {
    if (status == '20') {
      return true;
    } else {
      return false;
    }
  }

  bool isShowComment(String status) {
    if (status == '25') {
      return true;
    } else {
      return false;
    }
  }

  bool isShowLogistics(String status) {
    if (status == '20' || status == '25' || status == '30') {
      return true;
    } else {
      return false;
    }
  }

  String getOrderStatusSub() {
    if (entity.code == 0) {
      return '';
    }
    if (entity.data.status == '5') {
      return '剩余${entity.data.cancelMinute}分钟自动关闭';
    }
    if (entity.data.status == '10' ||
        entity.data.status == '11' ||
        entity.data.status == '15' ||
        entity.data.status == '20' ||
        entity.data.status == '25' ||
        entity.data.status == '30') {
      return '';
    }
    return '';
  }

  String getStatusIcon(String status) {
    print(status);
    switch (status) {
      case '5':
        return 'assets/images/icon_order_status_pay.png';
      case '10':
      case '11':
        return 'assets/images/icon_order_status_cancel.png';
      case '15':
      case '20':
        return 'assets/images/icon_order_status_send.png';
      case '25':
      case '30':
        return 'assets/images/icon_order_status_done.png';
    }
    return 'assets/images/icon_order_status_pay.png';
  }

  orderDetail() async {
    dynamic result = await Api.orderDetail(widget.orderNo);
    setState(() {
      entity = OrderDetailEntity.fromJson(result);
      orderStatusSub = getOrderStatusSub();
      orderStatusIcon = getStatusIcon(entity.data.status);
    });
  }

  orderSendNotice(String orderNo) async {
    dynamic result = await Api.orderSendNotice(orderNo);
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      EasyLoading.showToast("已提醒卖家发货，请耐心等待");
      orderDetail();
    }
  }

  orderCancel(String orderNo, int index) async {
    dynamic result = await Api.cancelOrder('$orderNo/$index');
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      EasyLoading.showToast("订单取消成功");
      orderDetail();
    }
  }

  refundOrderPost(String goodsId, String num, String reason) async {
    dynamic result = await Api.applyRefund({
      'type': '1',
      'order_no': widget.orderNo,
      'order_goods_id': goodsId,
      'refund_reason': reason,
      'num': num,
    });
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      EasyLoading.showToast("退款已申请，等待审核");
      orderDetail();
    }
  }

  cancelOrder(BuildContext context, String orderNo) async {
    Picker(
        adapter: PickerDataAdapter(
            pickerData: SingletonManager.instance.orderCancelReason, isArray: false),
        height: 350.w,
        headerDecoration: BoxDecoration(
          color: Config.colorWhite,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20.w),
            topRight: Radius.circular(20.w),
          ),
        ),
        itemExtent: 70.w,
        confirmText: '确定',
        cancelText: '取消',
        columnPadding: EdgeInsets.all(15.w),
        onConfirm: (picker, list) {
          // LogUtil.v(SingletonManager.instance.orderCancelReason[list[0]]);
          orderCancel(orderNo, list[0]);
        }).showModal(context, backgroundColor: Colors.transparent);
  }

  refundOrder(BuildContext context, String goodsId, String num) async {
    Picker(
        adapter: PickerDataAdapter(
            pickerData: List.generate(SingletonManager.instance.refundReason.length,
                (index) => SingletonManager.instance.refundReason[index].title),
            isArray: false),
        height: 350.w,
        headerDecoration: BoxDecoration(
          color: Config.colorWhite,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20.w),
            topRight: Radius.circular(20.w),
          ),
        ),
        itemExtent: 70.w,
        confirmText: '确定',
        cancelText: '取消',
        columnPadding: EdgeInsets.all(15.w),
        onConfirm: (picker, list) {
          // LogUtil.v(SingletonManager.instance.refundReason[list[0]]);
          refundOrderPost(goodsId, num, SingletonManager.instance.refundReason[list[0]].title);
        }).showModal(context, backgroundColor: Colors.transparent);
  }

  orderReceipt(String orderNo) async {
    var content = const Utf8Encoder().convert(payPwd);
    String md5pwd = "${md5.convert(content)}";

    dynamic result = await Api.orderReceipt({'order_no': orderNo, 'pay_pwd': md5pwd});
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      EasyLoading.showToast("收货成功");
      orderDetail();
    }
  }

  checkPayPwd() async {
    dynamic result = await Api.checkSetPayPwd();
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      String pay_pwd_exists = jsonResult['data']['pay_pwd_exists'];
      return pay_pwd_exists;
    }
    return "0";
  }

  checkExpress(String orderNo) async {
    dynamic result = await Api.expressMap(orderNo);
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      Routes.navigateTo(context, Routes.orderQueryExpress, params: {'orderNo': orderNo});
    }
  }

  getListItem(OrderDetailDataGoods item) {
    return Container(
      padding: EdgeInsets.only(top: 15.w),
      height: 175.w,
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(color: Config.colorDivider, width: 1.w)),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(right: 20.w),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: CachedNetworkImage(
                imageUrl: item.skuSnap.imgPath,
                fit: BoxFit.fill,
                width: 100.w,
                height: 100.w,
                errorWidget: (context, url, error) {
                  return Image.asset(
                    'assets/images/bg_default_400.png',
                    width: 100.w,
                    height: 100.w,
                    fit: BoxFit.fill,
                  );
                },
              ),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    width: 420.w,
                    child: Text(
                      item.skuSnap.goods.name,
                      overflow: TextOverflow.ellipsis,
                      softWrap: false,
                      style: TextStyle(color: (Config.colorText1), fontSize: 22.w),
                    ),
                  ),
                  Container(
                    alignment: Alignment.centerRight,
                    width: 120.w,
                    child: Text(
                      item.statusDetail,
                      softWrap: false,
                      style: TextStyle(color: Colors.red, fontSize: 22.w),
                    ),
                  ),
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 5.w),
                width: 540.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      item.skuSnap.specVal.toString(),
                      softWrap: true,
                      style: TextStyle(color: (Config.colorText1), fontSize: 20.w),
                    ),
                    Text(
                      "X${item.buyNum}",
                      softWrap: true,
                      style: TextStyle(color: (Config.colorText1), fontSize: 20.w),
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 5.w),
                width: 540.w,
                child: Text(
                  CommonUtils.formatAmountUnit(
                      item.money, item.company, item.money1, item.company1),
                  softWrap: true,
                  style: TextStyle(color: (Config.colorText1), fontSize: 20.w),
                ),
              ),
              Container(
                width: 540.w,
                margin: EdgeInsets.only(top: 5.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Visibility(
                      //未发货的情况，出现退款按钮
                      visible:
                          entity.code == 1 && entity.data.status == '15' && entity.data.tag == '2',
                      //&& entity.data.tag == '2'
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 120.w,
                        height: 40.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () {
                            refundOrder(context, item.id, item.buyNum);
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(24.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '退款',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      //已发货的情况，出现退款/售后按钮
                      visible:
                          entity.code == 1 && entity.data.status == '20' && entity.data.tag == '2',
                      //&& entity.data.tag == '2'
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 150.w,
                        height: 40.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () {
                            Routes.navigateTo(context, Routes.orderRefundChoose, params: {
                              'orderNo': widget.orderNo,
                              'goodsId': item.id,
                              'img': item.skuSnap.imgPath,
                              'name': item.skuSnap.goods.name,
                              'spec': item.skuSnap.specVal.toString(),
                              'price': item.allPrice,
                              'num': item.buyNum,
                            }).then((value) {
                              if (value != null && value['result'] == 1) {
                                orderDetail();
                              }
                            });
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(24.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '退款/售后',
                            style: TextStyle(fontSize: 17.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  _showDialogForPinInput(context, orderNo) {
    payPwd = '';
    AwesomeDialog(
      context: context,
      animType: AnimType.SCALE,
      dialogType: DialogType.NO_HEADER,
      body: Container(
        padding: EdgeInsets.only(top: 5.w, left: 20.w, right: 20.w),
        child: Column(
          children: [
            Text(
              "请输入支付密码",
              style: TextStyle(color: Config.colorText1, fontSize: 30.sp),
            ),
            Padding(
              padding: EdgeInsets.only(top: 30.w),
            ),
            PinCodeTextField(
              appContext: context,
              length: 6,
              obscureText: true,
              // controller: textEditingController,
              onChanged: (value) {},
              keyboardType: TextInputType.phone,
              onCompleted: (value) {
                payPwd = value;
              },
              // pinTheme: PinTheme(
              //   shape: PinCodeFieldShape.box, //输入框的形状
              //   borderRadius: const BorderRadius.all(Radius.circular(10)),
              //   // 其他可以设置可以自己参考，比如有输入文字的时候输入框是什么颜色，不能输入的时候是什么颜色等
              // ),
            ),
            InkWell(
              onTap: () {
                Routes.navigateTo(context, Routes.mineChangePassword,
                    params: {'pageTitle': '修改支付密码'});
              },
              child: Container(
                alignment: Alignment.centerRight,
                padding: EdgeInsets.only(right: 2.w),
                child: Text(
                  "忘记密码?",
                  style: TextStyle(
                      color: Config.colorText1,
                      fontSize: 25.sp,
                      decoration: TextDecoration.underline),
                ),
              ),
            ),
          ],
        ),
      ),
      title: '',
      desc: '',
      btnOkText: '确定',
      btnOkColor: Config.colorMain,
      btnOkOnPress: () {
        // print('oncompletd $payPwd');
        if (payPwd.isEmpty) {
          return;
        }
        orderReceipt(orderNo);
      },
    ).show();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('订单详情'),
        centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        color: Config.colorBg2,
        height: double.infinity,
        child: Stack(
          children: [
            SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Column(
                children: [
                  Container(
                    //订单状态显示
                    margin: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
                    height: 100.w,
                    padding: EdgeInsets.symmetric(horizontal: 25.w),
                    decoration: BoxDecoration(
                      color: Config.colorWhite,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    child: Row(
                      children: [
                        Image.asset(
                          orderStatusIcon,
                          width: 35.w,
                          height: 35.w,
                          errorBuilder: (ctx, err, stacktrace) => Image.asset(
                            'assets/images/bg_default_400.png',
                            width: 35.w,
                            height: 35.w,
                            fit: BoxFit.fill,
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 25.w),
                        ),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              entity.code == 1 ? entity.data.statusStr : '',
                              style:
                                  TextStyle(fontSize: 26.sp, height: 1.2, color: Config.colorText1),
                            ),
                            Visibility(
                              visible: orderStatusSub.isNotEmpty,
                              child: Text(
                                entity.code == 1 ? orderStatusSub : '',
                                style: TextStyle(fontSize: 20.sp, height: 1.8, color: Colors.red),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Stack(
                    children: [
                      Container(
                        //收货信息
                        margin: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
                        decoration: BoxDecoration(
                          color: Config.colorWhite,
                          borderRadius: BorderRadius.circular(10.w),
                        ),
                        width: double.infinity,
                        height: 134.w,
                        padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 22.w),
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "收货人：${entity.code == 1 ? entity.data.addressSnap.username : ''}",
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.addressSnap.mobile : '',
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                              ],
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 20.w),
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                // Icon(
                                //   Icons.location_on_outlined,
                                //   size: 32.w,
                                //   color: Config.colorText1,
                                // ),
                                Expanded(
                                  child: Text(
                                    '收货地址：${entity.code == 1 ? entity.data.addressSnap.address : ''}',
                                    softWrap: false,
                                    style: TextStyle(fontSize: 22.sp),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 147.w),
                        child: ClipRRect(
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(12.w),
                            bottomRight: Radius.circular(12.w),
                          ),
                          child: Image.asset(
                            "assets/images/ic_reciver_divider.png",
                            fit: BoxFit.fill,
                            width: double.infinity,
                            height: 6.w,
                          ),
                        ),
                      ),
                    ],
                  ),
                  Container(
                    //商品信息
                    margin: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
                    decoration: BoxDecoration(
                      color: Config.colorWhite,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    width: double.infinity,
                    padding: EdgeInsets.only(left: 25.w, right: 25.w, top: 20.w),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            CachedNetworkImage(
                              imageUrl: entity.code == 1 ? entity.data.store.logo : '',
                              fit: BoxFit.fill,
                              width: 32.w,
                              height: 32.w,
                              errorWidget: (context, url, error) {
                                return Image.asset(
                                  'assets/images/bg_default_400.png',
                                  width: 32.w,
                                  height: 32.w,
                                  fit: BoxFit.fill,
                                );
                              },
                            ),
                            Padding(
                              padding: EdgeInsets.only(left: 10.w),
                            ),
                            Expanded(
                              child: Text(
                                entity.code == 1 ? entity.data.store.name : '',
                                style: TextStyle(fontSize: 24.sp),
                              ),
                            ),
                          ],
                        ),
                        Container(
                          width: double.infinity,
                          margin: EdgeInsets.only(top: 10.w),
                          height: entity.code == 1 ? (entity.data.goods.length * 175.w) : 175.w,
                          child: ListView.builder(
                            physics: const NeverScrollableScrollPhysics(),
                            itemCount: entity.code == 1 ? entity.data.goods.length : 0,
                            scrollDirection: Axis.vertical,
                            itemBuilder: (BuildContext context, int index) {
                              return getListItem(entity.data.goods[index]);
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    //价格和支付信息
                    margin: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
                    decoration: BoxDecoration(
                      color: Config.colorWhite,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    child: Column(
                      children: [
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(25.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "商品总价",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                              Text(
                                entity.code == 1
                                    ? CommonUtils.formatAmountUnit(
                                        entity.data.goodsMoney,
                                        entity.data.goodsCompany,
                                        entity.data.goodsMoney1,
                                        entity.data.goodsCompany1)
                                    : '',
                                style: TextStyle(fontSize: 24.sp),
                              ),
                            ],
                          ),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(25.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "物流费用",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                              Text(
                                "${(entity.code == 1 && entity.data.storeExpressMoney.isNotEmpty) ? entity.data.storeExpressMoney : '0'}金券",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                            ],
                          ),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(25.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "订单总价",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                              Text(
                                entity.code == 1
                                    ? CommonUtils.formatAmountUnit(
                                        entity.data.allMoney,
                                        entity.data.allCompany,
                                        entity.data.allMoney1,
                                        entity.data.allCompany1)
                                    : '',
                                style: TextStyle(fontSize: 24.sp, color: Colors.redAccent),
                              ),
                            ],
                          ),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Visibility(
                          visible: entity.code == 1 &&
                              (entity.data.status == '5' ||
                                  entity.data.status == '15' ||
                                  entity.data.status == '20' ||
                                  entity.data.status == '25' ||
                                  entity.data.status == '30'),
                          child: Container(
                            width: double.infinity,
                            padding: EdgeInsets.all(25.w),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  (entity.code == 1 &&
                                          entity.data.status != "5" &&
                                          entity.data.status != "10" &&
                                          entity.data.status != "11")
                                      ? "已支付"
                                      : "需支付",
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                                Text(
                                  entity.code == 1
                                      ? CommonUtils.formatAmountUnit(
                                          entity.data.money,
                                          entity.data.company,
                                          entity.data.money1,
                                          entity.data.company1)
                                      : '',
                                  style: TextStyle(fontSize: 24.sp, color: Colors.redAccent),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        height: 60.w,
                        width: 250.w,
                        margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 20.w),
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () {
                            if (entity.code == 0) {
                              return;
                            }
                            Clipboard.setData(ClipboardData(text: entity.data.store.contact));
                            EasyLoading.showToast("${entity.data.store.contact}复制成功");
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorText1, width: 1.5.w),
                            ),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.chat_outlined,
                                color: Config.colorText1,
                                size: 26.w,
                              ),
                              Padding(
                                padding: EdgeInsets.only(left: 5.w),
                              ),
                              Text(
                                '联系商家',
                                style: TextStyle(
                                    fontSize: 20.sp, height: 1.5, color: Config.colorText1),
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                  Container(
                    //订单号和订单时间
                    margin: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
                    decoration: BoxDecoration(
                      color: Config.colorWhite,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    child: Column(
                      children: [
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(25.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "订单编号",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                              Text(
                                entity.code == 1 ? entity.data.orderNo : '',
                                style: TextStyle(fontSize: 24.sp),
                              ),
                            ],
                          ),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(25.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "创建时间",
                                style: TextStyle(fontSize: 24.sp),
                              ),
                              Text(
                                entity.code == 1 ? entity.data.goods[0].createdAt : '',
                                style: TextStyle(fontSize: 24.sp),
                              ),
                            ],
                          ),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.payTime.isNotEmpty,
                          child: Container(
                            width: double.infinity,
                            padding: EdgeInsets.all(25.w),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "付款时间",
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.payTime : '',
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                              ],
                            ),
                          ),
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.payTime.isNotEmpty,
                          child: Divider(
                            color: Config.colorDivider,
                            height: 2.w,
                            thickness: 2.w,
                          ),
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.deliveryTime.isNotEmpty,
                          child: Container(
                            width: double.infinity,
                            padding: EdgeInsets.all(25.w),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "发货时间",
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.deliveryTime : '',
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                              ],
                            ),
                          ),
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.deliveryTime.isNotEmpty,
                          child: Divider(
                            color: Config.colorDivider,
                            height: 2.w,
                            thickness: 2.w,
                          ),
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.confirmTime.isNotEmpty,
                          child: Container(
                            width: double.infinity,
                            padding: EdgeInsets.all(25.w),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "成交时间",
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.confirmTime : '',
                                  style: TextStyle(fontSize: 24.sp),
                                ),
                              ],
                            ),
                          ),
                        ),
                        Visibility(
                          visible: entity.code == 1 && entity.data.confirmTime.isNotEmpty,
                          child: Divider(
                            color: Config.colorDivider,
                            height: 2.w,
                            thickness: 2.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 120.w,
                  ),
                ],
              ),
            ),
            Positioned(
              bottom: 0,
              child: Container(
                decoration: const BoxDecoration(color: Config.colorBg),
                height: 100.w,
                width: 750.w,
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 30.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Visibility(
                      visible: isShowCancelBtn(entity.code == 1 ? entity.data.status : '0'),
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 140.w,
                        height: 55.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () => {cancelOrder(context, widget.orderNo)},
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(28.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorText3, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '取消订单',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorText3),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: isShowTixingBtn(entity.code == 1 ? entity.data.status : '0') &&
                          entity.data.isShowNoticeSend == '1',
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 140.w,
                        height: 55.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () => {orderSendNotice(widget.orderNo)},
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(28.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorText3, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '提醒发货',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorText3),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: isShowPayBtn(entity.code == 1 ? entity.data.status : '0'),
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 120.w,
                        height: 55.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () => {
                            Routes.navigateTo(context, Routes.orderCashier, params: {
                              'amount': CommonUtils.formatAmountUnit(entity.data.money,
                                  entity.data.company, entity.data.money1, entity.data.company1),
                              'orderNo': entity.data.orderNo,
                              'payApiType': entity.data.tag,
                              'skuId': entity.data.goods[0].skuSnap.id
                            }).then((value) {
                              orderDetail();
                            })
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(28.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '付款',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: isShowComment(entity.code == 1 ? entity.data.status : '0'),
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 120.w,
                        height: 46.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () {
                            SingletonManager.instance.currentOrderDetail = entity;
                            Routes.navigateTo(context, Routes.publishComment, params: {
                              'sourceType': "2",
                            }).then((value) {
                              orderDetail();
                            });
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(24.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '评价',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: isShowReceiptBtn(entity.code == 1 ? entity.data.status : '0'),
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 140.w,
                        height: 46.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () async {
                            // DialogManager.showDefaultDialog(context, "是否确认收货", () async {
                            //   orderReceipt(widget.orderNo);
                            // })
                            String isCheck = await checkPayPwd();
                            if (isCheck == '1') {
                              // DialogManager.showDefaultDialog(
                              //   context,
                              //   "是否确认收货",
                              //   () async {
                              //
                              //   },
                              // );
                              _showDialogForPinInput(context, widget.orderNo);
                            } else {
                              //请用户去设置支付密码
                              DialogManager.showDefaultDialog(context, "你还未设置支付密码，请前往设置", () {
                                Routes.navigateTo(context, Routes.mineChangePassword,
                                    params: {'pageTitle': '修改支付密码'});
                              });
                            }
                          },
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(24.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '确认收货',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: isShowLogistics(entity.code == 1 ? entity.data.status : '0'),
                      child: Container(
                        margin: EdgeInsets.only(left: 15.w),
                        width: 140.w,
                        height: 46.w,
                        child: ElevatedButton(
                          clipBehavior: Clip.antiAlias,
                          onPressed: () => {checkExpress(widget.orderNo)},
                          style: ButtonStyle(
                            elevation: MaterialStateProperty.all(0),
                            backgroundColor: MaterialStateProperty.all(Colors.white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(24.w),
                              ),
                            ),
                            side: MaterialStateProperty.all(
                              BorderSide(color: Config.colorMain, width: 2.w),
                            ),
                          ),
                          child: Text(
                            '查看物流',
                            style: TextStyle(fontSize: 18.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
